package com.yun.baymax.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yun.baymax.R;
import com.yun.baymax.bean.adapter.ColorEntry;
import com.yun.lib.entry.ThemeChanged;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import static com.yun.lib.utils.SPUtils.get;
import static com.yun.lib.utils.SPUtils.put;

/**
 * Created by Administrator on 2017/6/12.
 */

public class ChangeThemeAdapter extends RecyclerView.Adapter<ChangeThemeAdapter.MyHolder> {
    List<ColorEntry> datas = new ArrayList<>();
    Context context;
    private LayoutInflater inflater;
    private String mTheme;


    public ChangeThemeAdapter(Context context, List<ColorEntry> datas) {
        this.context = context;
        this.datas = datas;
        mTheme = (String) get(context, "theme", "");
        inflater = LayoutInflater.from(context);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_theme, parent, false);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        final ColorEntry colorEntry = datas.get(position);
        holder.colorName.setTextColor(colorEntry.getColor());
        holder.colorName.setText(colorEntry.getColorName());
        holder.isUsed.setSelected(mTheme.equals(colorEntry.getColorName()));
        if (position == 0 && TextUtils.isEmpty(mTheme))
            holder.isUsed.setText("使用中");
        else
            holder.isUsed.setText(mTheme.equals(colorEntry.getColorName()) ? "使用中" : "使用");
        holder.egColor.setImageResource(colorEntry.getEg());
        holder.isUsed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                put(context, "theme", colorEntry.getColorName());
                EventBus.getDefault().post(new ThemeChanged());
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private ImageView egColor;
        private TextView colorName;
        private TextView isUsed;
        private int position;

        public MyHolder(View view) {
            super(view);
            egColor = (ImageView) view.findViewById(R.id.eg_color);
            colorName = (TextView) view.findViewById(R.id.color_name);
            isUsed = (TextView) view.findViewById(R.id.is_used);
        }
    }
}